package com.ds.controller;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
 * @program: springmvc
 * @description: \
 * @author: Dawson.Zhang
 * @create: 2020-04-07
 **/
public class TestFilter extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("验证是否登录" );
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user" );
        if (Optional.ofNullable(user).isPresent()){
            System.out.println(user);

            return true;
        }else{
            System.out.println("user是空的");
            response.sendRedirect("hello");
            return false;
        }
    }
}
